package com.zzyl.common.utils;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class PDFUtil {

    public static String pdfToString(InputStream is) {
        PDDocument pdDocument = null;
        try {
            pdDocument = PDDocument.load(is);

            PDFTextStripper pdfTextStripper = new PDFTextStripper();

            String text = pdfTextStripper.getText(pdDocument);
            return text;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                pdDocument.close();
                is.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        FileInputStream is=new FileInputStream("C:\\Users\\小辉\\Desktop\\体检报告-刘爱国-男-69岁.pdf");
        String content=pdfToString(is);
        System.out.println(content);
    }

}
